package co.unal.visor.controller;

import java.applet.Applet;
import java.awt.*;

import javax.swing.JFrame;

public class histograma1 extends Applet {
	public void init() {
		setVisible(true);
		setLayout(null);
		resize(900, 500);
		txt1 = new TextField();
		txt2 = new TextField();
		txt3 = new TextField();
		txt4 = new TextField();
		btnHisto = new Button("Histograma");
		btnPoli = new Button("Poligono");
		btnlimpiar = new Button("Limpiar");
		add(txt1);
		add(txt2);
		add(txt3);
		add(txt4);
		add(btnHisto);
		add(btnPoli);
		add(btnlimpiar);
		ubicar_objetos();
	}

	public void ubicar_objetos() {
		txt1.setBounds(100, 35, 50, 20);
		txt2.setBounds(100, 70, 50, 20);
		txt3.setBounds(100, 105, 50, 20);
		txt4.setBounds(100, 140, 50, 20);
		btnHisto.setBounds(10, 210, 100, 25);
		btnPoli.setBounds(10, 245, 100, 25);
		btnlimpiar.setBounds(10, 280, 100, 25);
	}

	public void paint(Graphics g) {
		this.setBackground(Color.BLACK);
		g.setColor(Color.YELLOW);
		g.drawString("Histograma Porcentual (Ingresar Valores Entre 1 y 1200)",
				400, 15);
		g.drawString("Valor 1 :", 10, 50);
		g.drawString("Valor 2 :", 10, 85);
		g.drawString("Valor 3 :", 10, 120);
		g.drawString("Valor 4 :", 10, 155);
		g.drawString("Opciones:", 10, 190);
		g.setColor(Color.BLUE);
		g.draw3DRect(200, 30, 670, 450, true);
		g.drawRect(5, 200, 115, 120);
		g.setColor(Color.WHITE);
		g.drawString("El sisguiente sistema consta", 10, 350);
		g.drawString("de la evaluacion de valores que ", 10, 365);
		g.drawString("permite graficar un histograma", 10, 380);
		g.drawString("y a la vez permite verificar el", 10, 395);
		g.drawString("poligono de frecuencias", 10, 410);
	}

	public void Trazar_Histograma(int a1, int b1, int c1, int d1, int r) {
		Graphics g = getGraphics();
		int a2 = 0;
		int b2 = 0;
		int c2 = 0;
		int d2 = 0;
		int p = 0;
		if (r > 400) {
			p = r / 400 + 1;
			a2 = 450 - a1 / p;
			b2 = 450 - b1 / p;
			c2 = 450 - c1 / p;
			d2 = 450 - d1 / p;
		} else if (r < 100) {
			p = 3;
			a2 = 450 - a1 * p;
			b2 = 450 - b1 * p;
			c2 = 450 - c1 * p;
			d2 = 450 - d1 * p;
		} else {
			a2 = 450 - a1;
			b2 = 450 - b1;
			c2 = 450 - c1;
			d2 = 450 - d1;
		}
		g.setColor(Color.LIGHT_GRAY);
		g.fillRect(250, a2, 90, 480 - a2);
		g.drawRect(250, a2, 90, 480 - a2);
		g.setColor(Color.BLUE);
		g.fillRect(350, b2, 90, 480 - b2);
		g.drawRect(350, b2, 90, 480 - b2);
		g.setColor(Color.DARK_GRAY);
		g.fillRect(450, c2, 90, 480 - c2);
		g.drawRect(450, c2, 90, 480 - c2);
		g.setColor(Color.ORANGE);
		g.fillRect(550, d2, 90, 480 - d2);
		g.drawRect(550, d2, 90, 480 - d2);
	}

	public void Trazar_Poligono(int a1, int b1, int c1, int d1, int r) {
		Graphics g = getGraphics();
		int a2 = 0;
		int b2 = 0;
		int c2 = 0;
		int d2 = 0;
		int p = 0;
		if (r > 400) {
			p = r / 400 + 1;
			a2 = 480 - a1 / p;
			b2 = 480 - b1 / p;
			c2 = 480 - c1 / p;
			d2 = 480 - d1 / p;
		} else if (r < 100) {
			p = 3;
			a2 = 480 - a1 * p;
			b2 = 480 - b1 * p;
			c2 = 480 - c1 * p;
			d2 = 480 - d1 * p;
		} else {
			a2 = 480 - a1;
			b2 = 480 - b1;
			c2 = 480 - c1;
			d2 = 480 - d1;
		}
		g.setColor(Color.BLUE);
	
		g.drawLine(20, a2, 250, 480);
		g.drawLine(250, a2, 350, b2);
		g.drawLine(350, b2, 450, c2);
		g.drawLine(450, c2, 550, d2);
		g.drawLine(550, d2, 550, 480);
	}

	public boolean action(Event evento, Object o) {
		Graphics g = getGraphics();
		if (evento.target instanceof Button) {
			if (evento.target == btnlimpiar) {
				repaint();
				txt1.setText("");
				txt2.setText("");
				txt3.setText("");
				txt4.setText("");
			} else {
				int r = 0;
				int a = Math.abs(Integer.valueOf(txt1.getText()).intValue());
				int b = Math.abs(Integer.valueOf(txt2.getText()).intValue());
				int c = Math.abs(Integer.valueOf(txt3.getText()).intValue());
				int d = Math.abs(Integer.valueOf(txt4.getText()).intValue());
				if (a < 1 || a > 1200) {
					txt1.setText("");
					a = 0;
				} else if (b < 1 || b > 1200) {
					txt2.setText("");
					b = 0;
				} else if (c < 1 || c > 1200) {
					txt3.setText("");
					c = 0;
				} else if (d < 1 || d > 1200) {
					txt4.setText("");
					d = 0;
				} else {
					if (a > r)
						r = a;
					if (b > r)
						r = b;
					if (c > r)
						r = c;
					if (d > r)
						r = d;
					if (evento.target == btnHisto)
						Trazar_Histograma(a, b, c, d, r);
					else
						Trazar_Poligono(a, b, c, d, r);
				}
			}
			return true;
		} else {
			return true;
		}
	}

	private TextField txt1;
	private TextField txt2;
	private TextField txt3;
	private TextField txt4;
	private Button btnHisto;
	private Button btnlimpiar;
	private Button btnPoli;
}